package com.team20.map_demo.controller;


import com.team20.map_demo.domain.Street;
import com.team20.map_demo.service.StreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/Street")
public class StreetController {
    @Autowired
    private StreetService streetService;

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public String streetPage(Model model){
        List<Street> streets = streetService.listStreet();
        model.addAttribute("streets",streets);
        return "street-list";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addStreetPage(Model model){
        model.addAttribute("street", new Street());
        return "street-add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String savaStreet(Street street ){
        int updated = streetService.savaStreet(street);
        return "redirect:/Street/search";
    }

    @GetMapping(value = "/edit")
    public String editStreetPage(@RequestParam("id")String id, Model model){
        Street street = streetService.findStreetByPrimaryKey(id);
        model.addAttribute("street",street);
        return "street-add";
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteStreet(@RequestParam("id") String id){
        int deleted = streetService.deleteStreetByPrimaryKey(id);
        return deleted > 0;
    }


    @RequestMapping("/streetTypeCount")
    public String selectStreetTypeCount(String streetName,Model model){
        List<Street> streetList = streetService.selectStreetTypeCount(streetName);
        model.addAttribute("street",streetList);
        return "statistics";
    }

    @RequestMapping("/findAllStreet")
    @ResponseBody
    public List<Street> findStreet(){
        List<Street> streets = streetService.findStreetByName();
        return streets;
    }
    @RequestMapping("/selectAllRegion")
    public List<Region> selectAllRegion(Model model){
        List<Region> regions = regionService.selectAllRegion();
        model.addAttribute("regions",regions);
        return regions;
    }
@RequestMapping("/exportStreetInfo.action")
    public void exportStreetInfo(HttpServletResponse response ) throws IOException{
        response.setCharacterEncoding("UTF-8");
        List<Street> list =    regionService.queryAllStreetInfoToExcel();
        System.out.println(list);
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("街道表");

        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("街道Id");
        titleRow.createCell(1).setCellValue("姓名");
        titleRow.createCell(2).setCellValue("行政区Id");
        //遍历将数据放到excel列中
        for (Street street : list) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(region.getId());
            dataRow.createCell(1).setCellValue(region.getName());
            dataRow.createCell(2).setCellValue(region.gettRId());
        }
                  /*   // 设置下载时客户端Excel的名称
             String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=" + filename);  */

        // 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("行政区规划名单".getBytes(),"iso-8859-1") + ".xls");


        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}
